package io.renren.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.R;
import io.renren.modules.shop.entity.CustomerEntity;
import io.renren.modules.shop.entity.MerchantEntity;
import io.renren.modules.shop.entity.RegisterEntity;
import io.renren.modules.shop.service.CustomerService;
import io.renren.modules.shop.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/shop")
public class RegisterController {

    @Autowired
    CustomerService customerService;
    @Autowired
    MerchantService merchantService;
    /**
     * 登录
     */
    @RequestMapping("/cRegister")
    public R URegister(@RequestBody RegisterEntity user){

        QueryWrapper<CustomerEntity> queryWrapper = new QueryWrapper<CustomerEntity>();
        queryWrapper.lambda().eq(CustomerEntity::getMobilePhone,user.getMobilePhone());
        //用户信息
        CustomerEntity customer = customerService.getOne(queryWrapper);

        if(customer!=null){
            return R.error(1,"用户已存在");
        }else {
            CustomerEntity customerEntity = new CustomerEntity();
            customerEntity.setLoginName(user.getUserName());
            customerEntity.setPassword(user.getPassword());
            customerEntity.setMobilePhone(user.getMobilePhone());
            customerEntity.setUserStats(1);
            customerEntity.setCreateTime(new Date());
            customerEntity.setGender("男");
            customerEntity.setUserName("letao");
            customerService.save(customerEntity);
        }
        return R.ok();
    }
    /**
     * 登录
     */
    @RequestMapping("/mRegister")
    public R MRegister(@RequestBody RegisterEntity user){

        QueryWrapper<MerchantEntity> queryWrapper = new QueryWrapper<MerchantEntity>();
        queryWrapper.lambda().eq(MerchantEntity::getMobilePhone,user.getMobilePhone());
        //用户信息
        MerchantEntity merchant = merchantService.getOne(queryWrapper);

        if(merchant!=null){
            return R.error("用户已存在");
        }else {
            MerchantEntity merchantEntity = new MerchantEntity();
            merchantEntity.setCreateTime(new Date());
            merchantEntity.setLoginName(user.getUserName());
            merchantEntity.setMerchantName(user.getUserName()+"的商品平台");
            merchantEntity.setMerchantRealName(user.getUserName());
            merchantEntity.setMobilePhone(user.getMobilePhone());
            merchantEntity.setPassword(user.getPassword());
            merchantService.save(merchantEntity);
        }
        return R.ok();
    }
}
